package cn.iune.web.backcenter.controllers;



import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;

import cn.iune.backcenter.authen.ISaOppersonService;
import cn.iune.backcenter.author.IAuthorizeService;
import cn.iune.backcenter.author.ISaOpauthorizeService;
import cn.iune.backcenter.author.ISaOporgService;
import cn.iune.backcenter.author.ISaOppermissionService;
import cn.iune.backcenter.author.ISaOproleService;
import cn.iune.backcenter.author.SaOporg;
import cn.iune.backcenter.author.sysinfo.IBillSettingService;
import cn.iune.backcenter.author.sysinfo.ISysActionService;
import cn.iune.backcenter.author.sysinfo.ISysInfoService;
import cn.iune.backcenter.author.sysinfo.ISysService;


@Controller
@RequestMapping(value="/backcenter")
public class BackcenterController {
	
	private final Logger log = LogManager.getLogger(this);
	
	
	@Autowired
	private ISaOppersonService saOppersonService;
	
	@Autowired
	private ISysInfoService sysInfoService;
	
	
	@Autowired  
    private ISaOporgService saOporgService;
	@Autowired
	private ISaOproleService saOproleService;
	@Autowired
	private ISaOppermissionService saOppermissionService;
	@Autowired
	private ISaOpauthorizeService saOpauthorizeService;
	@Autowired
 	private ISysActionService sysActionService;
	@Autowired
 	private IAuthorizeService authorizeService;
	
	@Autowired
	private ISysService sysService;
	
	@Autowired
 	private IBillSettingService billSettingService;
	
	



    //用户中心
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView homeView(HttpServletRequest request){
		JSONObject person = (JSONObject) request.getAttribute("person");
		String sName = person.getString("cUsername");
		String personID = person.getString("userId");
		String sLoginName = person.getString("sLoginName");
		log.info("/backcenter/frame-->>"+sName);
//		String COMPUTERNAME = (String) request.getServletContext().getAttribute("COMPUTERNAME");
//		System.out.println("COMPUTERNAME-->>"+COMPUTERNAME);
//		String test1 = (String) request.getServletContext().getAttribute("test1");
//		System.out.println("test1-->>"+test1);
//		JSONArray billtypes = (JSONArray) request.getServletContext().getAttribute("billtypes");
//		System.out.println("billtypes-->>"+billtypes.size());

		Map<String,String> map=new HashMap<String,String>();  
        map.put("activityId", "aactivityId");  
        map.put("sName", sName);
        map.put("personID", personID);
        map.put("sLoginName", sLoginName);
		ModelAndView mav = new ModelAndView("/backcenter/frame", map);
//		mav.addObject("person",principals);
		return mav;
	}
	
	
    //用户中心
	@RequestMapping(value="/frame2",method=RequestMethod.GET)
	public ModelAndView homeView2(HttpServletRequest request){
//		JSONObject person = (JSONObject) request.getAttribute("person");
//		String sName = person.getString("cUsername");
//		String personID = person.getString("userId");
//		String sLoginName = person.getString("sLoginName");

		Map<String,String> map=new HashMap<String,String>();  
        map.put("activityId", "aactivityId");  
//        map.put("sName", sName);
//        map.put("personID", personID);
//        map.put("sLoginName", sLoginName);
		ModelAndView mav = new ModelAndView("/backcenter/frame2", map);
//		mav.addObject("person",principals);
		return mav;
	}
	
	
    //信息面板
	@RequestMapping(value="/home/center",method=RequestMethod.GET)
	public ModelAndView centerView(){
		ModelAndView mav = new ModelAndView("/backcenter/center/center");
		return mav;
	}
	
	//获取顶级（创始组织）组织列表
	@RequestMapping(value = "/org/getMyOgnList", method = RequestMethod.GET)
	@ResponseBody
	public List<SaOporg> getMyOgnList(HttpServletRequest request) {
		JSONObject person = (JSONObject) request.getAttribute("person");
		String userId = person.getString("userId");
//		System.out.println("userId-->"+userId);
		return saOporgService.getOgnsBysCreatorFID(userId);
	}
	
	//获取顶级（我参与的组织）组织列表
	@RequestMapping(value = "/org/getOgnList", method = RequestMethod.GET)
	@ResponseBody
	public List<SaOporg> getOgnList(HttpServletRequest request) {
		JSONObject person = (JSONObject) request.getAttribute("person");
		String userId = person.getString("userId");
//		System.out.println("userId-->"+userId);
		return saOporgService.getOgnsByPersonID(userId);
	}
	
	//获取人员成员列表
	@RequestMapping(value = "/org/getPsmList", method = RequestMethod.GET)
	@ResponseBody
	public List<SaOporg> getPsmList(HttpServletRequest request) {
		JSONObject person = (JSONObject) request.getAttribute("person");
		String userId = person.getString("userId");
		return saOporgService.getByPersonID(userId);
	}

	
	

	/*
	//付款单//angularjs的ui-router好像无法支持post请求方式
	@RequestMapping(value="/payment/single",method=RequestMethod.POST)
	public ModelAndView paymentSinglePOST(){
		ModelAndView mav = new ModelAndView("/backcenter/payment/single");
		return mav;
	}
	 */

	




	/*
	//付款后跳转
	@RequestMapping(value="/alipayReturn",method=RequestMethod.GET)
	public ModelAndView alipay_return(HttpServletRequest request,HttpServletResponse response) throws AlipayApiException, UnsupportedEncodingException{
//		ModelAndView mav = new ModelAndView("/backcenter/payment/return_url");
		ModelAndView mav = new ModelAndView("/backcenter/payment/my_return");
		
		//获取支付宝GET过来反馈信息
		Map<String,String> params = new HashMap<String,String>();
		Map<String,String[]> requestParams = request.getParameterMap();
		for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用
			//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
			params.put(name, valueStr);
		}
		System.out.println("params-->"+params);
		boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
		System.out.println("signVerified-->"+signVerified);
		//——请在这里编写您的程序（以下代码仅作参考）——
		if(signVerified) {
			//商户订单号
			String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
		
			//支付宝交易号
			String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
		
			//付款金额
			String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8");
			
			//out.println("trade_no:"+trade_no+"<br/>out_trade_no:"+out_trade_no+"<br/>total_amount:"+total_amount);
			
			mav.addObject("sign_status","验签成功");
			
			String[] strs = out_trade_no.split("/");
			out_trade_no = strs[0];
//			String trade_enterprise_no = strs[1];
			String trade_enterprise_no = strs[1].split("\\.")[0];
			
			mav.addObject("out_trade_no",out_trade_no);
			mav.addObject("trade_enterprise_no",trade_enterprise_no);
			mav.addObject("trade_no", trade_no);
			mav.addObject("total_amount", total_amount);
			
			String app_id = new String(request.getParameter("app_id").getBytes("ISO-8859-1"),"UTF-8");
			System.out.println("app_id-->"+app_id);
			mav.addObject("app_id", app_id);
			
		}else {
			//out.println("验签失败");
			mav.addObject("sign_status","验签失败");
		}
		//——请在这里编写您的程序（以上代码仅作参考）——
		
		
		return mav;
	}
	*/

	

	

//	@RequestMapping(value="/sysInfoOrg/queryJOIN",method=RequestMethod.POST)
//	@ResponseBody
//	public JSONObject sysInfoOrgQueryJOIN(@RequestBody JSONObject data) {
//		return sysInfoOrgUtil.queryJOIN(data);
//		
//	}

	/*
	@RequestMapping(value="/sysInfoOrg/queryTreeByOrgID",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysInfoOrgQueryTreeByOrgID(@RequestBody JSONObject data) {
		return sysInfoUtil.queryTreeByOrgID(data);
		
	}
	*/

	

//	@RequestMapping(value="/sysInfoOrg/copyList",method=RequestMethod.GET)
//	@ResponseBody
//	public void sysInfoOrgCopyList(@RequestParam(value="id",required=true) String id, HttpServletRequest request) throws DocumentException{
//		JSONObject person = (JSONObject) request.getAttribute("person");
//		String userId = person.getString("userId");
//		authorizeService.copyList(id,userId);
//	}
	
	@RequestMapping(value="/sysInfo/selectMulti",method=RequestMethod.GET)
	public ModelAndView sysInfoSelectMulti(){
		
		ModelAndView mav = new ModelAndView("/backcenter/sysInfo/selectMulti");
		return mav;
	}
	
	

	//公司列表
	@RequestMapping(value="/ogn/list",method=RequestMethod.GET)
	public ModelAndView ognListView(){
		ModelAndView mav = new ModelAndView("/backcenter/ogn/list");
		return mav;
	}
	//配置公司
	@RequestMapping(value="/ogn/center",method=RequestMethod.GET)
	public ModelAndView ognCenterView(){
		ModelAndView mav = new ModelAndView("/backcenter/ogn/center");
		return mav;
	}
	//创建公司
	@RequestMapping(value="/org/single",method=RequestMethod.GET)
	public ModelAndView ognSingleView(){
		ModelAndView mav = new ModelAndView("/backcenter/org/single");
		return mav;
	}
	
	//编辑公司详细描述
	@RequestMapping(value="/ogn/description",method=RequestMethod.GET)
	public ModelAndView ognDescriptionView(){
		ModelAndView mav = new ModelAndView("/backcenter/ogn/description");
		return mav;
	}
	
    
	//组织机构
	@RequestMapping(value="/org/tree",method=RequestMethod.GET)
	public ModelAndView orgTreeView(){
		ModelAndView mav = new ModelAndView("/backcenter/org/tree");
		return mav;
	}
    
	
	//////////////////////////////////////////////////////////////////////////
	@RequestMapping(value = "/org/tree/query", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject orgQuery(@RequestBody JSONObject data) {
		return saOporgService.queryData(data);
	}

	@RequestMapping(value = "/org/tree/save", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject orgSave(@RequestBody JSONObject object) {
		return saOporgService.saveData(object);
	}

	@RequestMapping(value = "/org/tree/delete", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject orgDelete(@RequestBody JSONObject data) {
		return saOporgService.deleteData(data);
	}

	@RequestMapping(value="/org/tree/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject orgCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject person = (JSONObject) request.getAttribute("person");
		JSONObject params = data;//JSONObject.parseObject(data.toJSONString());
		//增加登录信息
		//缺省值
		JSONArray defaultValues = params.getJSONArray("defaultValues");
		//循环缺省值（多行）
		for(int i=0;i<defaultValues.size();i++) {
			JSONObject rowValue = defaultValues.getJSONObject(i);
			//循环列
			//循环缺省值（单行）
			for(String col: rowValue.keySet()) {
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
				if(":getCurrentDate()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDate());
				}
//				if(":getCurrentPsmFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFID());
//				}
//				if(":getCurrentPsmFName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFName());
//				}
				if(":getCurrentDateTime()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDateTime());
				}
				if(":getCurrentPersonCode()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsercode"));
				}
				if(":getCurrentPersonName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsername"));
				}
				if(":getCurrentPersonID()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("userId"));
				}
				if(":getsLoginName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("sLoginName"));
				}
//				if(":getCurrentOgnFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsFID());
//				}
//				if(":getCurrentOgnLongName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsLongName());
//				}
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
			}
			
		}
		JSONObject json = saOporgService.newData(data);
		return json;
	}
	/////////////////////////////////////////////////////////////////////////////////
	
	
	
	/*
	// 新建人员（建立人员成员）
	@RequestMapping(value = "/org/tree/newPersonMember", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject orgNewPersonMember(@RequestBody JSONObject data, HttpServletRequest request)  {
		JSONObject person = (JSONObject) request.getAttribute("person");
		String userId = person.getString("userId");
		data.put("sCreatorFID", userId);
		return backcenterService.newPersonMember(data);
	}
	*/
	
	// 分配人员（建立人员成员）
	@RequestMapping(value = "/org/tree/addPersonMember", method = RequestMethod.POST)
	@ResponseBody
	public void orgAddPersonMember(@RequestBody JSONObject data, HttpServletRequest request)  {
		JSONObject person = (JSONObject) request.getAttribute("person");
		String userId = person.getString("userId");
		
		String orgID = data.getString("orgID");
		String personID = data.getString("personID");
		String personCode = data.getString("personCode");
		String personName = data.getString("personName");
//		System.out.println("orgID-->"+orgID+"--personID-->"+personID);
		
		saOporgService.addPersonMember(orgID, personID, personCode, personName, userId);
	}
	
	// 添加创始人（建立人员成员）
	@RequestMapping(value = "/org/tree/addFounderMember", method = RequestMethod.POST)
	@ResponseBody
	public void orgAddFounderMember(@RequestBody JSONObject data, HttpServletRequest request)  {
		JSONObject person = (JSONObject) request.getAttribute("person");
		String userId = person.getString("userId");
		
		String orgID = data.getString("orgID");
		
		String personID = userId;
		String personCode = person.getString("cUsercode");
		String personName = person.getString("cUsername");
//		System.out.println("orgID-->"+orgID+"--personID-->"+personID);
		
		saOporgService.addPersonMember(orgID, personID, personCode, personName, userId);
	}
	
    //帐号设置
	@RequestMapping(value="/password/page",method=RequestMethod.GET)
	public ModelAndView passwordSetup(){
		ModelAndView mav = new ModelAndView("/backcenter/password/page");
		return mav;
	}
	
	//选择公海人员（云人员）注册用户
	@RequestMapping(value="/person/selectSingle",method=RequestMethod.GET)
	public ModelAndView personSelectSingle(){
		ModelAndView mav = new ModelAndView("/backcenter/person/selectSingle");
		return mav;
	}

	@RequestMapping(value = "/person/selectSingle/query", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject personSelectQuery(@RequestBody JSONObject data)  {
		return saOppersonService.queryData(data);
	}

	
	
	@RequestMapping(value="/person/list",method=RequestMethod.GET)
	public ModelAndView personListView(){
		ModelAndView mav = new ModelAndView("/backcenter/person/list");
		return mav;
	}
	
	@RequestMapping(value="/person/single",method=RequestMethod.GET)
	public ModelAndView personSingleView(){
		ModelAndView mav = new ModelAndView("/backcenter/person/single");
		return mav;
	}
	@RequestMapping(value = "/person/list/query", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject personQuery(@RequestBody JSONObject data)  {
		return saOppersonService.queryData(data);
	}

	@RequestMapping(value = "/person/list/save", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject personSave(@RequestBody JSONObject object)  {
		//增加唯一性检查功能
		/*
		if(saOppersonService.checkUnique(object)) {
			return saOppersonService.saveData(object);
		}else {
			JSONObject json = new JSONObject();
			json.put("code", "999");
			json.put("status", "唯一性检查未通过，数据未保存！");
			return json;
		}
		*/
		return saOppersonService.saveData(object);
	}

	@RequestMapping(value = "/person/list/delete", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject personDelete(@RequestBody JSONObject data) {
		return saOppersonService.deleteData(data);
	}

	@RequestMapping(value="/person/list/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject personCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject person = (JSONObject) request.getAttribute("person");
		JSONObject params = data;//JSONObject.parseObject(data.toJSONString());
		//增加登录信息
		//缺省值
		JSONArray defaultValues = params.getJSONArray("defaultValues");
		//循环缺省值（多行）
		for(int i=0;i<defaultValues.size();i++) {
			JSONObject rowValue = defaultValues.getJSONObject(i);
			//循环列
			//循环缺省值（单行）
			for(String col: rowValue.keySet()) {
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
				if(":getCurrentDate()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDate());
				}
//				if(":getCurrentPsmFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFID());
//				}
//				if(":getCurrentPsmFName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFName());
//				}
				if(":getCurrentDateTime()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDateTime());
				}
				if(":getCurrentPersonCode()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsercode"));
				}
				if(":getCurrentPersonName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsername"));
				}
				if(":getCurrentPersonID()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("userId"));
				}
				if(":getsLoginName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("sLoginName"));
				}
//				if(":getCurrentOgnFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsFID());
//				}
//				if(":getCurrentOgnLongName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsLongName());
//				}
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
			}
		}
		JSONObject json = saOppersonService.newData(data);
		return json;
	}
	
	
	


	//组织机构排序
	@RequestMapping(value="/org/sortView",method=RequestMethod.GET)
	public ModelAndView sortView(){
		ModelAndView mav = new ModelAndView("/backcenter/org/sort");
		return mav;
	}



	//排序
	@RequestMapping(value = "/org/sort", method = RequestMethod.POST)
	@ResponseBody
	public void orgSort(@RequestBody JSONObject data){
		String parentID = data.getString("parentID");
		JSONArray jsonArray =  data.getJSONArray("ids");
//		System.out.println("jsonArray-->"+jsonArray);
		List<String> ids = new ArrayList<String>();
		for(int i=0; i<jsonArray.size();i++){
			ids.add(jsonArray.getString(i));
		}
//		System.out.println("ids-->"+ids);
		saOporgService.sortGroupInfo(parentID, ids);
	}
	
	//添加角色
	@RequestMapping(value="/role/list",method=RequestMethod.GET)
	public ModelAndView roleView(){
		ModelAndView mav = new ModelAndView("/backcenter/role/list");
		return mav;
	}

	@RequestMapping(value = "/role/single", method = RequestMethod.GET)
	public ModelAndView roleSingle() {

		ModelAndView mav = new ModelAndView("/backcenter/role/single");
		return mav;
	}

	
	
	@RequestMapping(value = "/role/list/query", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject roleQuery(@RequestBody JSONObject data)  {
		return saOproleService.queryData(data);
	}

	@RequestMapping(value = "/role/list/save", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject roleSave(@RequestBody JSONObject object)  {
		return saOproleService.saveData(object);
	}

	@RequestMapping(value = "/role/list/delete", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject roleDelete(@RequestBody JSONObject data) {
		return saOproleService.deleteData(data);
	}

	@RequestMapping(value="/role/list/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject roleCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject person = (JSONObject) request.getAttribute("person");
		System.out.println("controller.roleCreate.data-->>"+data.toJSONString());
		JSONObject params = data;//JSONObject.parseObject(data.toJSONString());
		//增加登录信息
		//缺省值
		JSONArray defaultValues = params.getJSONArray("defaultValues");
		//循环缺省值（多行）
		for(int i=0;i<defaultValues.size();i++) {
			JSONObject rowValue = defaultValues.getJSONObject(i);
			//循环列
			//循环缺省值（单行）
			for(String col: rowValue.keySet()) {
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
				if(":getCurrentDate()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDate());
				}
//				if(":getCurrentPsmFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFID());
//				}
//				if(":getCurrentPsmFName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFName());
//				}
				if(":getCurrentDateTime()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDateTime());
				}
				if(":getCurrentPersonCode()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsercode"));
				}
				if(":getCurrentPersonName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsername"));
				}
				if(":getCurrentPersonID()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("userId"));
				}
				if(":getsLoginName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("sLoginName"));
				}
//				if(":getCurrentOgnFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsFID());
//				}
//				if(":getCurrentOgnLongName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsLongName());
//				}
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
			}
			
		}

		JSONObject json = saOproleService.newData(data);
		return json;
	}
	
	
	
	
	//配置权限
	@RequestMapping(value="/permission/list",method=RequestMethod.GET)
	public ModelAndView permission(){
		ModelAndView mav = new ModelAndView("/backcenter/permission/list");
		return mav;
	}

	@RequestMapping(value = "/permission/list/query", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject permissionQuery(@RequestBody JSONObject data) {
		return saOppermissionService.queryData(data);
	}

	@RequestMapping(value = "/permission/list/save", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject permissionSave(@RequestBody JSONObject object) {
		return saOppermissionService.saveData(object);
	}

	@RequestMapping(value = "/permission/list/delete", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject permissionDeletes(@RequestBody JSONObject data) {
		return saOppermissionService.deleteData(data);
	}

	@RequestMapping(value="/permission/list/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject permissionCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject person = (JSONObject) request.getAttribute("person");
		JSONObject params = data;//JSONObject.parseObject(data.toJSONString());
		//增加登录信息
		//缺省值
		JSONArray defaultValues = params.getJSONArray("defaultValues");
		//循环缺省值（多行）
		for(int i=0;i<defaultValues.size();i++) {
			JSONObject rowValue = defaultValues.getJSONObject(i);
			//循环列
			//循环缺省值（单行）
			for(String col: rowValue.keySet()) {
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
				if(":getCurrentDate()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDate());
				}
//				if(":getCurrentPsmFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFID());
//				}
//				if(":getCurrentPsmFName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFName());
//				}
				if(":getCurrentDateTime()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDateTime());
				}
				if(":getCurrentPersonCode()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsercode"));
				}
				if(":getCurrentPersonName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsername"));
				}
				if(":getCurrentPersonID()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("userId"));
				}
				if(":getsLoginName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("sLoginName"));
				}
//				if(":getCurrentOgnFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsFID());
//				}
//				if(":getCurrentOgnLongName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsLongName());
//				}
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
			}
			
		}
		JSONObject json = saOppermissionService.newData(data);
		return json;
	}
	
	
	

	// 添加权限
	@RequestMapping(value = "/permission/list/add", method = RequestMethod.POST)
	@ResponseBody
	public void permissionAdd(@RequestBody JSONObject data)  {
		authorizeService.addFuncPerm(data);
	}

	// 添加actions
	@RequestMapping(value = "/permission/list/updateActionsByID", method = RequestMethod.POST)
	@ResponseBody
	public void permissionUpdateActionsByID(@RequestBody JSONObject data)  {
		System.out.println("data-->" + data);
		String sID = data.getString("id");
		String sActions = data.getString("actions");
		// JSONObject sActions = data.getJSONObject("actions");
		// System.out.println("sActionsObject-->"+sActionsObject);
		// System.out.println("sActions.toString()-->"+sActions.toString());
		// System.out.println("sActions.toJSONString()-->"+sActions.toJSONString());
		// String sActions = null;
		// if(sActionsObject!=null){
		// sActions = sActionsObject.toJSONString();
		// }
		saOppermissionService.updateActionsByID(sID, sActions);
	}

	// 添加数据权限
	@RequestMapping(value = "/permission/list/add1", method = RequestMethod.POST)
	@ResponseBody
	public void permissionAdd1(@RequestBody JSONObject data)  {
		authorizeService.addDataPerm(data);
	}
	

	@RequestMapping(value = "/role/selectMulti", method = RequestMethod.GET)
	public ModelAndView roleSelectMulti() {

		ModelAndView mav = new ModelAndView("/backcenter/role/selectMulti");
		return mav;
	}
	
	//按组织授权
	@RequestMapping(value="/orgauth/list",method=RequestMethod.GET)
	public ModelAndView orgauthView(){
		ModelAndView mav = new ModelAndView("/backcenter/orgauth/list");
		return mav;
	}

	
	
	@RequestMapping(value = "/orgauth/list/query", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject authorizeQuery(@RequestBody JSONObject data) {
		return saOpauthorizeService.queryData(data);
	}

	@RequestMapping(value = "/orgauth/list/save", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject authorizeSave(@RequestBody JSONObject object)  {
		return saOpauthorizeService.saveData(object);
	}

	@RequestMapping(value = "/orgauth/list/delete", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject authorizeDelete(@RequestBody JSONObject data) {
		return saOpauthorizeService.deleteData(data);
	}

	@RequestMapping(value="/orgauth/list/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject authorizeCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject person = (JSONObject) request.getAttribute("person");
		JSONObject params = data;//JSONObject.parseObject(data.toJSONString());
		//增加登录信息
		//缺省值
		JSONArray defaultValues = params.getJSONArray("defaultValues");
		//循环缺省值（多行）
		for(int i=0;i<defaultValues.size();i++) {
			JSONObject rowValue = defaultValues.getJSONObject(i);
			//循环列
			//循环缺省值（单行）
			for(String col: rowValue.keySet()) {
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
				if(":getCurrentDate()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDate());
				}
//				if(":getCurrentPsmFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFID());
//				}
//				if(":getCurrentPsmFName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentPsm().getsFName());
//				}
				if(":getCurrentDateTime()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDateTime());
				}
				if(":getCurrentPersonCode()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsercode"));
				}
				if(":getCurrentPersonName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("cUsername"));
				}
				if(":getCurrentPersonID()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("userId"));
				}
				if(":getsLoginName()".equals(rowValue.get(col))) {
					rowValue.put(col, person.getString("sLoginName"));
				}
//				if(":getCurrentOgnFID()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsFID());
//				}
//				if(":getCurrentOgnLongName()".equals(rowValue.get(col))) {
//					rowValue.put(col, SecurityService.getCurrentOgn().getsLongName());
//				}
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
			}
			
		}
		JSONObject json = saOpauthorizeService.newData(data);
		return json;
	}

	
	
	// 添加授权
	@RequestMapping(value = "/orgauth/list/add", method = RequestMethod.POST)
	@ResponseBody
	public void authorizeAdd(@RequestBody JSONObject data, HttpServletRequest request)  {
		JSONObject person = (JSONObject) request.getAttribute("person");
		String userId = person.getString("userId");
//		String cUsercode = person.getString("cUsercode");
		String cUsername = person.getString("cUsername");
		data.put("personID", userId);
		data.put("personName", cUsername);
		authorizeService.addAuthorize(data);
	}
	
	//选择动作
	@RequestMapping(value="/sysAction/selectMulti",method=RequestMethod.GET)
	public ModelAndView selectAction(){
		ModelAndView mav = new ModelAndView("/backcenter/sysAction/selectMulti");
		return mav;
	}
	
	

	
	
	
	
    /*
	// 获取当前人员成员
	@RequestMapping(value = "/org/getMainPersonMember", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject getMainPersonMember() {
		
		 * JSONObject json = new JSONObject(); json.put("code", "001"); SaOporg
		 * obj =
		 * saOporgUtil.getPersonMember(SecurityService.getCurrentPerson());
		 * if(obj!=null){ json = (JSONObject) JSON.toJSON(obj); }
		 

		MapMessage map = new MapMessage(true);
		SaOporg obj = saOporgService.getMainPersonMember(SecurityService.getCurrentPerson());
		if (obj != null) {
			JSONObject data = (JSONObject) JSON.toJSON(obj);
			map.setData(data);
		} else {
			map.setStatusFlag(MapMessage.STATUSINFO_FAILURE_FLAG);
			map.setStatusMsg(MapMessage.STATUSINFO_FAILURE_MSG);
			// map.setStatusCode("525212");
			map.setStatusInfo("查询对象为空！");
		}
		return map.toJSONObject();
	}

	//获取人员成员信息及其所属公司
	@RequestMapping(value = "/org/getPersonMemberInfo", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject getPersonMemberInfo(@RequestParam(value="id",required=true) String id) {

		MapMessage map = new MapMessage(true);
		SaOporg obj = saOporgService.getByID(id);
		if (obj != null) {
			JSONObject data = new JSONObject();
			data.put("psmID", obj.getsID());
			data.put("psmCode", obj.getsCode());
			data.put("psmName", obj.getsName());
			data.put("psmFID", obj.getsFID());
			data.put("psmFCode", obj.getsFCode());
			data.put("psmFName", obj.getsFName());
			data.put("psmLName", obj.getsLongName());

			SaOporg ogn = obj;
			while(!ogn.getsOrgKindID().equals("ogn")){
				ogn = saOporgService.getByID(ogn.getsParent());
			}
			data.put("ognFID", ogn.getsFID());
			data.put("ognLName", ogn.getsLongName());
			map.setData(data);
		} else {
			map.setStatusFlag(MapMessage.STATUSINFO_FAILURE_FLAG);
			map.setStatusMsg(MapMessage.STATUSINFO_FAILURE_MSG);
			// map.setStatusCode("525212");
			map.setStatusInfo("查询对象为空！");
		}
		return map.toJSONObject();
	}
	
	@RequestMapping(value = "/org/getPersonMemberInfoByFID", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject getPersonMemberInfoByFID(@RequestParam(value="id",required=true) String id) {

		MapMessage map = new MapMessage(true);
		SaOporg obj = saOporgService.getByFID(id);
		if (obj != null) {
			JSONObject data = new JSONObject();
			data.put("psmID", obj.getsID());
			data.put("psmCode", obj.getsCode());
			data.put("psmName", obj.getsName());
			data.put("psmFID", obj.getsFID());
			data.put("psmFCode", obj.getsFCode());
			data.put("psmFName", obj.getsFName());
			data.put("psmLName", obj.getsLongName());

			SaOporg ogn = obj;
			while(!ogn.getsOrgKindID().equals("ogn")){
				ogn = saOporgService.getByID(ogn.getsParent());
			}
			data.put("ognFID", ogn.getsFID());
			data.put("ognLName", ogn.getsLongName());
			map.setData(data);
		} else {
			map.setStatusFlag(MapMessage.STATUSINFO_FAILURE_FLAG);
			map.setStatusMsg(MapMessage.STATUSINFO_FAILURE_MSG);
			// map.setStatusCode("525212");
			map.setStatusInfo("查询对象为空！");
		}
		return map.toJSONObject();
	}

	// 获取当前人员所属组织列表
	@RequestMapping(value = "/org/getPersonOgnList", method = RequestMethod.GET)
	@ResponseBody
	public List<SaOporg> getPersonOgnList() {
		//只保留sOrgKindID=='ogn'的数据
		List<SaOporg> list = saOporgService.getPersonOrgs(SecurityService.getCurrentPerson());
		List<SaOporg> ognList = new ArrayList<SaOporg>();
		for(SaOporg org : list){
			if(org.getsOrgKindID().equals("ogn")){
				ognList.add(org);
			}
		}
		return ognList;
	}
	*/

	

	//应用设置（树形）
	@RequestMapping(value="/sysInfo/tree",method=RequestMethod.GET)
	public ModelAndView sysInfoView(){
		ModelAndView mav = new ModelAndView("/backcenter/sysInfo/tree");
		return mav;
	}
    
	
	/////////////////////////////////////////////////////////////////////////
	@RequestMapping(value="/sysInfo/tree/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysInfoQuery(@RequestBody JSONObject data) {
		return sysInfoService.queryData(data);
	}

	@RequestMapping(value="/sysInfo/tree/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysInfoSave(@RequestBody JSONObject data)  {
		return sysInfoService.saveData(data);
	}

//	@RequestMapping(value="/sysInfo/delete",method=RequestMethod.GET)
//	@ResponseBody
//	public void sysInfoDelete(@RequestParam(value="id",required=true) String id) {
//			sysInfoService.delete(id);
//	}
	
	@RequestMapping(value="/sysInfo/tree/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysInfoDelete(@RequestBody JSONObject data) {
		return sysInfoService.deleteData(data);
	}
	
	@RequestMapping(value="/sysInfo/tree/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysInfoCreate(@RequestBody JSONObject data) {
		JSONObject json = sysInfoService.newData(data);
		return json;
	}
	/////////////////////////////////////////////////////////////////////////
	
	

	@RequestMapping(value="/sysInfo/deleteAll",method=RequestMethod.GET)
	@ResponseBody
	public void sysInfoDeleteAll(){
		sysService.deleteAll();
	}
	
	//应用设置排序
	@RequestMapping(value="/sysInfo/sortView",method=RequestMethod.GET)
	public ModelAndView sysInfoSortView(){
		ModelAndView mav = new ModelAndView("/backcenter/sysInfo/sort");
		return mav;
	}
	
	//排序
	@RequestMapping(value = "/sysInfo/sort", method = RequestMethod.POST)
	@ResponseBody
	public void sysInfoSort(@RequestBody JSONObject data){
		String parentID = data.getString("parentID");
		JSONArray jsonArray =  data.getJSONArray("ids");
//		System.out.println("jsonArray-->"+jsonArray);
		List<String> ids = new ArrayList<String>();
		for(int i=0; i<jsonArray.size();i++){
			ids.add(jsonArray.getString(i));
		}
//		System.out.println("ids-->"+ids);
		sysInfoService.sortGroupInfo(parentID, ids);
	}
	
	//移动
	@RequestMapping(value="/sysInfo/moveView",method=RequestMethod.GET)
	public ModelAndView sysInfoMoveView(){
		ModelAndView mav = new ModelAndView("/backcenter/sysInfo/move");
		return mav;
	}
	//移动
	@RequestMapping(value = "/sysInfo/move", method = RequestMethod.POST)
	@ResponseBody
	public void sysInfoMove(@RequestBody JSONObject data){
		String rowID = data.getString("rowID");
		String oldParentID = data.getString("oldParentID");
		String newParentID = data.getString("newParentID");
		String appId = data.getString("appId");
		sysInfoService.move(rowID, oldParentID, newParentID, appId);
	}
	
	@RequestMapping(value="/sysInfo/importXML",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysInfoImportXML(@RequestBody JSONObject data) throws DocumentException  {
		sysService.importXML(data);
		return new JSONObject(0);
	}


	@RequestMapping(value="/sysInfo/list/getListAddOrg",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getListAddOrg(@RequestParam String appId, @RequestParam String orgID) {
		return sysInfoService.getAppListAddOrg(appId, orgID);
	}

	
	/////////////////////////////////////////////////////////////////////////


	//单据参数设置
	@RequestMapping(value="/billSetting/list",method=RequestMethod.GET)
	public ModelAndView billSettingInfoView(){
		ModelAndView mav = new ModelAndView("/backcenter/billSetting/list");
		return mav;
	}
	
	
	@RequestMapping(value="/billSetting/list/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject billSettingQuery(@RequestBody JSONObject data)  {
		return billSettingService.queryData(data);
	}

	@RequestMapping(value="/billSetting/list/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject billSettingSave(@RequestBody JSONObject data)  {
		return billSettingService.saveData(data);
	}

//	@RequestMapping(value="/backcenter/billSetting/delete",method=RequestMethod.GET)
//	@ResponseBody
//	public void billSettingDelete(@RequestParam(value="id",required=true) String id) throws DocumentException, IOException{
//		billSettingService.delete(id);
//	}
	
	@RequestMapping(value="/billSetting/list/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject billSettingDelete(@RequestBody JSONObject data) {
		return billSettingService.deleteData(data);
	}
	
	@RequestMapping(value="/billSetting/list/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject billSettingCreate(@RequestBody JSONObject data) {
		JSONObject json = billSettingService.newData(data);
		return json;
	}
	

	//单据参数设置(edit)
	@RequestMapping(value="/billSetting/single",method=RequestMethod.GET)
	public ModelAndView billSettingInfoEdit(){
		ModelAndView mav = new ModelAndView("/backcenter/billSetting/single");
		return mav;
	}
	

	//动作设置
	@RequestMapping(value="/sysAction/list",method=RequestMethod.GET)
	public ModelAndView sysActionList(){
		ModelAndView mav = new ModelAndView("/backcenter/sysAction/list");
		return mav;
	}
	
	
	
	
	@RequestMapping(value="/sysAction/list/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysActionQuery(@RequestBody JSONObject data)  {
		return sysActionService.queryData(data);
	}

	@RequestMapping(value="/sysAction/list/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysActionSave(@RequestBody JSONObject data)  {
		return sysActionService.saveData(data);
	}

	@RequestMapping(value="/sysAction/list/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysActionDelete(@RequestBody JSONObject data) throws DocumentException, IOException{
		return sysActionService.deleteData(data);
	}
	
	@RequestMapping(value="/sysAction/list/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject sysActionCreate(@RequestBody JSONObject data) {
		JSONObject json = sysActionService.newData(data);
		return json;
	}
	
	
	
	
	//动作设置(edit)
	@RequestMapping(value="/sysAction/single",method=RequestMethod.GET)
	public ModelAndView sysActionInfoEdit(){
		ModelAndView mav = new ModelAndView("/backcenter/sysAction/single");
		return mav;
	}

	@RequestMapping(value="/sysAction/templateAdd",method=RequestMethod.POST)
	@ResponseBody
	public int sysActionTemplateAdd(@RequestBody JSONObject data)  {
		String systemFullID = data.getString("systemFullID");
		String billKind = data.getString("billKind");
		sysActionService.templateAdd(systemFullID,billKind);
		return 0;
	}
	
	
	
}
